package com.ilop.sthome.ui.activity.device;

import android.content.Intent;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;

import com.ilop.sthome.ui.activity.main.ContainLoginActivity;
import com.example.common.utils.LiveDataBus;
import com.ilop.sthome.common.CommonId;
import com.ilop.sthome.data.bean.AlarmNotice;
import com.ilop.sthome.data.event.EventAnswerOK;
import com.ilop.sthome.data.event.EventGatewayInfo;
import com.ilop.sthome.mvp.contract.device.DeviceSetContract;
import com.ilop.sthome.mvp.present.device.DeviceSetPresenter;
import com.ilop.sthome.ui.activity.main.MainActivity;
import com.ilop.sthome.ui.adapter.device.DeviceSetNoticeAdapter;
import com.ilop.sthome.utils.CoderUtils;
import com.siterwell.familywellplus.R;
import com.siterwell.familywellplus.databinding.ActivityDeviceSettingBinding;

import java.util.List;

/**
 * @author skygge
 * @date 2020-01-10.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：网关/设备设置
 */
public class DeviceSettingActivity extends ContainLoginActivity<DeviceSetPresenter, ActivityDeviceSettingBinding> implements DeviceSetContract.IView{

    private int mDeviceId;
    private String mDeviceName;
    private boolean isSubAlarm = false;
    private DeviceSetNoticeAdapter mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_device_setting;
    }

    @Override
    protected void initialize() {
        super.initialize();
        mDeviceId = getIntent().getIntExtra(CommonId.KEY_DEVICE_ID,0);
        mDeviceName = getIntent().getStringExtra(CommonId.KEY_DEVICE_NAME);
    }

    @Override
    protected void initPresent() {
        super.initPresent();
        mPresent = new DeviceSetPresenter(mContext, mDeviceName, mDeviceId);
    }

    @Override
    protected void initView() {
        super.initView();
        mDBind.tvGatewaySetTitle.setText(mDeviceId==0 ? getString(R.string.ali_gateway_setting) : getString(R.string.ali_subdevice_setting));
        mDBind.deviceUnbind.setText(mDeviceId == 0 ? getString(R.string.delete_gateway) : getString(R.string.delete_equipment));
        mAdapter = new DeviceSetNoticeAdapter(mContext);
        mDBind.rvNoticeList.setLayoutManager(new LinearLayoutManager(mContext));
        mDBind.rvNoticeList.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {
        super.initData();
        mPresent.showViewByDeviceId(mDeviceId);
    }

    @Override
    protected void initListener() {
        super.initListener();
        mDBind.ivGatewayBack.setOnClickListener(view -> finish());
        mDBind.replaceDevice.setOnClickListener(view -> mPresent.onReplaceDevice());
        mDBind.name.setOnClickListener(view -> mPresent.setDeviceName());
        mDBind.room.setOnClickListener(v -> mPresent.modifyRoom());
        mDBind.share.setOnClickListener(view -> mPresent.skipToShared());
        mDBind.ota.setOnClickListener(view -> mPresent.onRouterToOTA());
        mDBind.deviceUnbind.setOnClickListener(view -> mPresent.onUnBindDevice());
        mDBind.ivSubOff.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isSubAlarm != isChecked) {
                showProgressDialog();
                mPresent.setSubOffLinePush(isChecked ? "00" : "01");
            }
        });
    }

    @Override
    protected void initDataBus() {
        super.initDataBus();
        LiveDataBus.get().with(CommonId.ALARM_NOTICE, AlarmNotice.class).observe(this, alarmNotice -> {
            assert alarmNotice != null;
            mPresent.setDeviceNoticeEnabled(alarmNotice.getEventId(), !alarmNotice.isNoticeEnabled());
            showProgressDialog();
        });
        LiveDataBus.get().with(CommonId.KEY_EVENT_GATEWAY_INFO, EventGatewayInfo.class).observe(this, event -> {
            if (event!=null){
                Log.i(TAG, "onEventMainThread: gateway " +  event.getData_str2());
                dismissProgressDialog();
                String code = event.getData_str2().substring(2);
                this.isSubAlarm = "00".equals(code);
                mDBind.ivSubOff.setChecked(isSubAlarm);
            }
        });
        LiveDataBus.get().with(CommonId.KEY_ANSWER_OK, EventAnswerOK.class).observe(this, event -> {
            if (event!=null){
                int result = CoderUtils.getAnswerResult(event.getData_str1(), event.getData_str2());
                Log.i(TAG, "initDataBus: answerOK--" + result);
                if (result == 4){
                    mPresent.deleteSuccess();
                }else if (result == 0){
                    showToast(R.string.failed);
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresent.onRefreshView();
        mPresent.requestNewVersion();
        if (mDeviceId == 0) mPresent.getDeviceNoticeList();
    }

    @Override
    public void isGatewayView() {
        mDBind.replaceDevice.setVisibility(View.GONE);
    }

    @Override
    public void isSubDeviceView() {
        mDBind.ota.setVisibility(View.GONE);
        mDBind.share.setVisibility(View.GONE);
        mDBind.gatewaySet.setVisibility(View.GONE);
    }

    @Override
    public void isSharedDevice(boolean isGateway) {
        mDBind.ota.setVisibility(View.GONE);
        mDBind.share.setVisibility(View.GONE);
        mDBind.gatewaySet.setVisibility(isGateway ? View.VISIBLE : View.GONE);
        mDBind.deviceUnbind.setVisibility(isGateway ? View.VISIBLE : View.GONE);
        mDBind.room.setVisibility(View.GONE);
        mDBind.replaceDevice.setVisibility(View.GONE);
        mDBind.subDeviceOfflinePush.setVisibility(View.GONE);
    }

    @Override
    public void showDeviceName(String name) {
        mDBind.name.setDetailText(name);
    }

    @Override
    public void showCurrentRoom(String room) {
        mDBind.room.setDetailText(room);
    }

    @Override
    public void showNewVersion(boolean newVersion) {
        mDBind.ota.setNewUpdateVisibility(newVersion);
    }

    @Override
    public void showNoticeList(List<AlarmNotice> noticeList) {
        mAdapter.setList(noticeList);
    }

    @Override
    public void withoutNotice() {
        mAdapter.setList(null);
    }

    @Override
    public void deleteSuccess() {
        dismissProgressDialog();
        showToast(getString(R.string.delete_success));
        Intent intent = new Intent(mContext, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }

    @Override
    public void startActivityByIntent(Intent intent) {
        startActivity(intent);
    }

    @Override
    public void showProgress() {
        showProgressDialog();
    }

    @Override
    public void showToastMsg(String msg) {
        dismissProgressDialog();
        showToast(msg);
    }
}
